package com.feiyi.app.base;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by wuwei on 2017/12/26.
 * description : 新闻列表fragment父类
 */
public abstract class BaseFragment extends Fragment {
    public BaseActivity activity;

    private CompositeDisposable compositeDisposable;
    public Handler handler = new Handler();
    @Override
    public void onAttach(Activity activity) {
            super.onAttach(activity);
            this.activity = (BaseActivity) activity;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return getLayoutInflater(inflater);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //获取控件
        initView();
        //设置adapter
        initAdapter();
        //获取数据
        initDate();
    }

    public abstract View getLayoutInflater(LayoutInflater inflater);
    public abstract void initView();
    public abstract void initDate();
    public abstract void initAdapter();

    /**
     * @param disposable 添加事件队列
     */
    public void addDisposable(Disposable disposable) {
        if (compositeDisposable == null) {
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable.add(disposable);
    }

    /**
     * 销毁事件队列
     */
    public void dispose() {
        if (compositeDisposable != null) compositeDisposable.dispose();
    }
}
